$(function(){
    loginName=baseFunction.getUrlParameterValueByKey("loginName");
    cardNo="";//卡号
    getPoundage();
    getBankInfo();


    // 提现
    $("#putForward").on("click",function(){
        var inputValue=$("#bindMoney").val();
        var maxQuota=baseFunction.getUrlParameterValueByKey("maxQuota"); //最大金额
        if(inputValue< 10){ //提现最小额度
            $(".prompt").html("单笔提现最小金额为10元").show(0).delay(1500).hide(0);
            return;
        }else if(inputValue > 20000){//大于最大额度
            $(".prompt").html("单笔提现最大金额为20000元").show(0).delay(1500).hide(0).show(0).delay(1500).hide(0);
            return;
        }else if(inputValue > maxQuota){//大于本身额度
            $(".prompt").html("您的可提现余额不足").show(0).delay(1500).hide(0);
            return;
        }
        // 显示密码输入框
        $(".payPassword .password").val("");
        $(".payPassword").removeClass("hide").addClass("show"); 
        $("#confPay").click(function(){ //确认密码
            var current=baseFunction.getUrlParameterValueByKey("current");
            if(current==1){
                var biztype=baseFunction.getUrlParameterValueByKey("bizType");
                var virtid=baseFunction.getUrlParameterValueByKey("virtId");
                confPay(current,cardNo,biztype,virtid);
            }else{
                confPay(current,cardNo);
            } 
        })
    })
    // 没有绑卡的时候调转去绑卡
    $("#addBank").on("click",function(){
        baseFunction.verifyingRealName(function(){
            window.location.href="my_addBankCard.html?loginName=" + loginName + "";
        })
        
    })
})
/**
 * 获取汇率和手续费
 */
function getPoundage(){
    var queryType;
    var current=baseFunction.getUrlParameterValueByKey("current");
    var maxQuota=baseFunction.getUrlParameterValueByKey("maxQuota"); //最大金额
    if (current==2){
      queryType="4631"
    } else if (current==3){
      queryType = "4641"
    } else if (current == 1){  //账户余额 固定为一元
        $(".content .main > text").html("可提现金额" + maxQuota + "元，手续费" + 1.00+"元/笔");
    //   this.setData({
    //     poundage:"1.00",
    //     prompt: "可提现金额" + that.data.maxQuota + "元，手续费" + 1.00+"元/笔"
    //   })
      return;
    }
    var data={
      userName: baseFunction.encryption(loginName),
      queryType: queryType
    };
    baseFunction.netRequest("param",data,function(res){
      
      $(".content .main > text").html("可提现金额" + maxQuota + "元，手续费" + (JSON.parse(JSON.parse(res.data).result).cashFee).toFixed(2) + "元/笔");
    })
}
/**
 * 获取结算卡的信息初始化
 */
function getBankInfo() {
    var res=baseFunction.getUserInfo();
    var data = JSON.parse(JSON.parse(res).merch);
    if (!data.bankTypeName) { //没有绑定结算卡
        $(".bankCard .debitCard .right .hideunionPayCards").removeClass("hide").addClass("show");
        return;
    }
    var imgString; //图片路径
    for (var j = 0; j < bankCard.length; j++) {
        if (data.bankTypeName.indexOf(bankCard[j].bankName) != -1) {
            imgString = "../static/img/" + bankCard[j].bigLogo + ".png";
        }
    }
    var endCardNo=(data.accountno).slice(-4); //卡号尾数
    var bankTypeName=data.bankTypeName; //银行名字
    cardNo=data.accountno //卡号
    //dom
    $(".bankCard .debitCard .right .binded>img").attr("src",imgString);
    $(".bankCard .debitCard .right .binded>text").html(""+bankTypeName+"（<text>"+endCardNo+"</text>）");

    $(".bankCard .debitCard .right .binded").removeClass("hide").addClass("show");

}

/**
 * 确认支付 biztype virtid 只有在账户余额提现的时候要传
 */
function confPay(current,cardNo,biztype,virtid) {
    $(".prompt").html("提现中...").show(0);
    //1 验证密码是否正确
    var data = {
        userName: baseFunction.encryption(loginName),
        password: baseFunction.encryption(hexMD5($(".payPassword .password").val())),
    };
    var money=$("#bindMoney").val(); //提现的金额
    baseFunction.netRequest("validPayPwd", data, function (res) {
        
        $(".payPassword").removeClass("show").addClass("hide"); //隐藏支付蒙版
        if (res.code == 200) {
            //2 快捷下单
            if (current==1){ //账户余额提现
                var data = {
                    userName: baseFunction.encryption(loginName),
                    "type": current,
                    amount: baseFunction.encryption(money),
                    accountno: baseFunction.encryption(cardNo),
                    bizType: biztype,
                    virtId: virtid
                }
            } else if (current == 2 ||current == 3){ //分润余额 推广余额提现
                var data = {
                    userName: baseFunction.encryption(loginName),
                    "type": current,
                    amount: baseFunction.encryption(money),
                    accountno: baseFunction.encryption(cardNo),
                }
            }
            baseFunction.netRequest("withdraw", data, function (res) {
                
                console.log(res);
                $(".prompt").html(res.message).show(0).delay(1000).hide(0);
                if(res.code==200){ //成功
                    setTimeout(function(){
                        window.location.href="wallet_successfulWithdrawal.html"
                    },2000);
                }
            })
        } else {
            $(".prompt").html("密码错误！").show(0).delay(1000).hide(0);
        }
    })
}